﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace GBH.Models.Dao
{
    public class DaysellMinMaxDao
    {
        //public List<anonymous> GetAllActiveBranch()
        //{
        //    using (var db = new GBHEntities())
        //    {
        //        var daysell = from minmax in db.daysellMinMaxes
        //                       join branch in db.Branches on minmax.location_id equals branch.mprovtrans
        //                       join Mat in db.materials on minmax.product_id equals Mat.npibarcode
        //                       where minmax.isEnable
        //                       select new daysellMinMax { 
        //                       branchName = branch.mprovdesc,
        //                       isEnable = minmax.isEnable,
        //                       location_id = minmax.location_id,
        //                       matName = Mat.mnamethai,
        //                       max = minmax.max,
        //                       min = minmax.min,
        //                       product_id = minmax.product_id,
        //                       rop = minmax.rop,
        //                       updateDate = minmax.updateDate
        //                       };
               
        //        return daysell.ToList();
        //    }
        //}
        public bool DeleteSellMinMax(daysellMinMax MinMax)
        {
            using (var db = new GBHEntities())
            {
                int result = db.Database.ExecuteSqlCommand("DELETE FROM daysellMinMax WHERE product_id = @product_id  and location_id = @location_id ", new[] { new SqlParameter("@product_id", MinMax.product_id), new SqlParameter("@location_id", MinMax.location_id) });
                return result > 0;
            }
        }

        public bool SaveSellMinMaxs(List<daysellMinMax> minMaxs)
        {
            try
            {
                using (var db = new GBHEntities())
                {
                    int result = db.Database.ExecuteSqlCommand("DELETE FROM daysellMinMax");
                    db.daysellMinMaxes.AddRange(minMaxs);
                    db.SaveChanges();
                }

                return true;
            }
            catch (Exception ex)
            {
                string error = ex.Message;
                return false;
            }

        }

        public bool SaveSellMinMax(daysellMinMax minMax)
        {
            using (var db = new GBHEntities())
            {
                daysellMinMax minmaxExiting = db.daysellMinMaxes.Where(m => m.product_id == minMax.product_id & m.location_id == minMax.location_id).FirstOrDefault();
                if (minmaxExiting == null)
                {
                    db.daysellMinMaxes.Add(minMax);
                }
                else
                {
                    minmaxExiting.min = minMax.min;
                    minmaxExiting.max = minMax.max;
                    minmaxExiting.rop = minMax.rop;
                    minmaxExiting.isEnable = minMax.isEnable;
                }
                return db.SaveChanges() > 0;
            }
        }

        public bool InsertSellMinMax(daysellMinMax newMinMax)
        {
            using (var db = new GBHEntities())
            {
                if (newMinMax.isInsert == null || newMinMax.isInsert)
                {
                    db.daysellMinMaxes.Add(newMinMax);
                }
                else
                {
                    var existMinMax = db.daysellMinMaxes.Where(m => m.product_id == newMinMax.product_id && m.location_id == newMinMax.location_id).FirstOrDefault();
                    if (existMinMax != null)
                    {
                        existMinMax.min = newMinMax.min;
                        existMinMax.max = newMinMax.max;
                        existMinMax.rop = newMinMax.rop;
                        existMinMax.isEnable = newMinMax.isEnable;
                    }
                }
                return db.SaveChanges() > 0;
            }
        }
    }
}